package com.cn.springcloudwechat.serviceImpl.articleAndMenu;


import com.cn.springcloudwechat.mapper.mapper.articleAndMenu.YmArticleMapper;
import com.cn.springcloudwechat.mapper.mapper.articleAndMenu.YmSubjectMapper;
import com.cn.springcloudwechat.mapper.mapper.articleAndMenu.YmSubjectPackageMapper;
import com.cn.springcloudwechat.model.articleAndMenu.YmArticle;
import com.cn.springcloudwechat.model.articleAndMenu.YmSubject;
import com.cn.springcloudwechat.model.articleAndMenu.YmSubjectPackage;
import com.cn.springcloudwechat.service.articleAndMenu.YmSubjectPackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 专题包实现类
 *
 * @author: Administrator
 * @create: 2018/7/19 16:28
 */
@Service("ymSubjectPackageServiceImpl")
public class YmSubjectPackageServiceImpl implements YmSubjectPackageService {

    @Autowired
    YmSubjectPackageMapper ymSubjectPackageMapper;

    @Autowired
    YmSubjectMapper ymSubjectMapper;
    @Autowired
    YmArticleMapper ymArticleMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertYmSubjectPackageAndYmSubject(YmSubjectPackage ymSubjectPackage, List<Long> articleIdList) {
        // 添加专题包并获取主键
        Long subjectPackageId = ymSubjectPackageMapper.insertSelective(ymSubjectPackage);
        Integer listSize = Integer.valueOf(articleIdList.size());
        List<YmSubject> ymSubjects = getYmSubjects(articleIdList, subjectPackageId);
        ymSubjectMapper.insertYmSubjectList(ymSubjects);
    }

    @Override
    @Cacheable
    public List<YmSubjectPackage> getAllYmSubjectPackage() {
        return ymSubjectPackageMapper.getAllYmSubjectPackage();
    }

    @Override
    @Cacheable
    public YmSubjectPackage getYmSubjectPackageAndArticle(Long subjectPackageId) {
        YmSubjectPackage ymSubjectPackage = ymSubjectPackageMapper.selectByPrimaryKey(subjectPackageId);
        List<YmSubject> ymSubjects = ymSubjectMapper.getAllYmSubject(subjectPackageId);
        List<YmArticle> ymArticles = new ArrayList<YmArticle>();
        for (YmSubject ymSubject : ymSubjects) {
            YmArticle ymArticle = ymArticleMapper.selectByPrimaryKey(ymSubject.getArticleId());
            ymArticles.add(ymArticle);
        }
        ymSubjectPackage.setChildYmArticles(ymArticles);
        return ymSubjectPackage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateYmSubjectPackage(YmSubjectPackage ymSubjectPackage, List<Long> articleIdList) {
        //获取主题包 原文章idlist
        Long subjectPackageId = ymSubjectPackage.getSubjectPackageId();
        List<Long> articleIds = ymSubjectMapper.getSubjectArticleId(subjectPackageId);
        // 被排除掉的文章
        List<Long> oldDifferenceSetArticleIds = removeAll(articleIds, articleIdList);
        if (oldDifferenceSetArticleIds.size() > 0) {
            //剔除文章
            ymSubjectMapper.updateYmSubjectState(oldDifferenceSetArticleIds, subjectPackageId);
        }
        //新添加的文章
        List<Long> newDifferenceSetArticleIds = removeAll(articleIdList, articleIds);
        Integer listSize = newDifferenceSetArticleIds.size();
        if (listSize > 0) {
            //添加文章
            List<YmSubject> ymSubjects = getYmSubjects(articleIdList, subjectPackageId);
            ymSubjectMapper.insertYmSubjectList(ymSubjects);
        }
    }

    /**
     * 获取集合差集
     *
     * @param src
     * @param target
     * @return
     */
    private static List<Long> removeAll(List<Long> src, List<Long> target) {
        //大集合用linkedlist
        LinkedList<Long> result = new LinkedList<>(src);
        //小集合用hashset
        HashSet<Long> targetHash = new HashSet<>(target);
        //采用Iterator迭代器进行数据的操作
        Iterator<Long> iter = result.iterator();
        while (iter.hasNext()) {
            if (targetHash.contains(iter.next())) {
                iter.remove();
            }
        }
        return result;
    }

    /**
     * 遍历文章id并添加YmSubject
     *
     * @param articleIdList
     * @param subjectPackageId
     * @return
     */
    private static List<YmSubject> getYmSubjects(List<Long> articleIdList, Long subjectPackageId) {
        Integer listSize = articleIdList.size();
        List<YmSubject> ymSubjects = new ArrayList<YmSubject>(listSize);
        for (Long articleId : articleIdList) {
            YmSubject ymSubject = new YmSubject();
            ymSubject.setArticleId(articleId);
            ymSubject.setSubjectPackageId(subjectPackageId);
            ymSubjects.add(ymSubject);
        }
        return ymSubjects;
    }
}
